<?php


namespace app\admin\controller\task;


use app\admin\model\DimArea;
use app\common\controller\AdminController;
use app\common\model\DimCategory;
use think\App;

use app\common\model\TaskRelease;

class Release extends AdminController
{


    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new TaskRelease();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {

        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            try{
                $result = $this->model->getListInfo($where, $page, $limit, 'a.id');
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => $result['count'],
                    'data'  => $result['list'],
                ];
                return json($data);
            }catch (\Exception $e){
                _return(-1, '操作失败');
            }
        }
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            $post['release_uid'] = session('admin.id');

            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                echo $e->getMessage();die;
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        // 分类列表
        $conditions = [];
        $conditions[] = array('is_del', '=', DimCategory::IS_DELETE_NO);
        $conditions[] = array('type', '=', DimCategory::CATE_TYPE_TASK);
        $cate_tree_list = (new DimCategory)->getTree($conditions);

        $this->assignData([
            'cate_tree_list' => $cate_tree_list
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        // 分类列表
        $conditions = [];
        $conditions[] = array('is_del', '=', DimCategory::IS_DELETE_NO);
        $conditions[] = array('type', '=', DimCategory::CATE_TYPE_TASK);
        $cate_tree_list = (new DimCategory)->getTree($conditions);

        // 获取对应区域
        $area_list = model(DimArea::TABLE_NAME)->getList(['pid' => DimArea::DEFAULT_ID]);
        $township_list = model(DimArea::TABLE_NAME)->getList(['pid' => $row['area']]);
        $community_list = model(DimArea::TABLE_NAME)->getList(['pid' => $row['township']]);

        $this->assignData([
            'row' => $row,
            'area_list' => $area_list,
            'township_list' => $township_list,
            'community_list' => $community_list,
            'cate_tree_list' => $cate_tree_list
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {

        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $data = [];
            $data['is_del'] = TaskRelease::DELETE_YES;
            $save = $this->model->whereIn('id', $id)->update($data);
        } catch (\Exception $e) {
            echo $e->getMessage();die;
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }
}